<?php
namespace Manage\Controller;
use Org\Util\Date;
use Think\Controller;
class AdminController extends Controller {

    public function index(){
        //查询出全部的泵站、调蓄池、河道闸门
        $station_model = D('PumpingStation');
        $csdt_model = D('Csdt');
        $gate_model = D('Gate');
        $stations = $station_model->getAll();
        $csdts = $csdt_model->getAll();
        $gates = $gate_model->getAll();
        $this->assign('stations', $stations);
        $this->assign('csdts', $csdts);
        $this->assign('gates', $gates);
        $this->assign('stations_count', count($stations));
        $this->assign('csdts_count', count($csdts));
        $this->assign('gates_count', count($gates));
        $ser_total_flow = 0;
        $pump_pump_model = D('PumpPump');
        $pumps = array();
        foreach($stations as $station){
            $temp_pumps = $pump_pump_model->queryField('ps_id', $station['id']);
            foreach($temp_pumps as $pump){
                $pumps[] = $pump;
                $ser_total_flow += $pump['p_flow_meas'];
            }
        }
        $this->assign('pumps_count', count($pumps));
        $this->assign('flow_count', number_format($ser_total_flow));
        $this->display('all_view');
    }

    /**
     * 实时雨情查看
     */
    public function live_rain_report(){
        $this->display();
    }

    public function get_live_rain_report(){
        header("Content-type: text/html; charset=utf-8");
        $map = array_merge(get_page_query_map(I('get.')), get_page_query_map(I('post.')));
        $page = max(I('get.page'), 1);
        $rows_num = get_kv_val('admin_page_list_num')?get_kv_val('admin_page_list_num'):I('get.rows');
        $list = D('WeatherchangeLog')->get_live_rain_report($map, $page, $rows_num);
        echo json_encode($list);
//        dump($list);
    }

    public function eqm_fix_log(){
        $this->display();
    }

    public function uploadify(){
        if (!empty($_FILES)) {
            import("@.Think.UploadFile");
            $upload = new \Think\Upload();
            $upload->rootPath  = C('UPLOAD_ROOT_PATH');
            $upload->savePath = 'moduleimg/'.date('Y').'/'.date('m').'/'.date('d').'/';
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'bmp', 'doc', 'xls', 'mp4', 'avi', 'docx', 'xlsx');
            $upload->autoSub = false;
            $upload->saveRule = uniqid; //上传规则
            $info = $upload->upload();
            if(!$info){
                echo $this->error($upload->getError());//获取失败信息
            } else {
                //成功
                $fileArray = "";
                foreach ($info as $file) {
                    $fileArray = '/'.C('UPLOAD_ROOT_PATH') . $file['savepath'] . $file['savename'];
                }
                echo trim($fileArray);
            }
        }
    }

    /**
     * 模型的字段动态查询
     */
    public function get_field_array(){
        header("Content-type: text/html; charset=utf-8");
        if(!I('get.model')){
            echo 'error';
            exit;
        }
        $model = D(I('get.model'));
        $first_key = I('table_index')?I('table_index'):'';
        $field = I('table_field')?I('table_field'):false;
        echo json_encode($model->get_field_array($first_key, $field));
    }

    public function clear_error_record(){
        $record_models = array(
            array(
                'text' => '泵站泵机运行记录',
                'model' => 'PumpPumpRecord'
            ),
            array(
                'text' => '泵站格栅机运行记录',
                'model' => 'PumpMachineRecord'
            ),
            array(
                'text' => '调蓄池泵机运行记录',
                'model' => 'CsdtPumpRecord'
            ),
            array(
                'text' => '调蓄池格栅机运行记录',
                'model' => 'CsdtMachineRecord'
            ),
            array(
                'text' => '调蓄池闸门运行记录',
                'model' => 'CsdtGateRecord'
            ),
            array(
                'text' => '河闸运行记录',
                'model' => 'GateRecord'
            ),
        );
        $last_date = M('ClearErrorRecordLog')->order('last_date desc')->find();
        $last_date = $last_date?$last_date:'';
        $data['last_date'] = getNowTime();
        M('ClearErrorRecordLog')->add($data);
        foreach($record_models as $temp){
            D($temp['model'])->clear_error_record($last_date['last_date']);
            echo $temp['text'].'清理完成<br>';
            ob_flush();
            flush();
        }
        echo '清理完成<br>';
    }

    public function open_window_import(){
        $this->display();
    }
}